import request from '@/utils/request'
import { genParam } from '@/utils/encrypt'

export function getList(params) {
  const paramStr = genParam(params)

  return request({
    url: '/admin/announce/list',
    method: 'get',
    params
  })
}

export function getAccessList(params) {
  const paramStr = genParam(params)
  return request({
    url: '/admin/announce/list',
    method: 'get',
    params
  })
}

export function createAnnounce(param) {
  const params = {
    title: param.title,
    type: param.type,
    content: param.content,
    stats: param.stats,
    is_push: param.is_push
  }
  if (params.type === '1') {
    params.mobile = param.member_mobile
  }
  const paramStr = genParam(params)
  // console.log(paramStr)

  return request({
    url: '/admin/announce/create',
    method: 'post',
    data: paramStr
  })
}

export function statsAnnounce(param) {
  return request({
    url: '/admin/announce/' + param.id + '/info',
    method: 'get'
  })
}

export function editAnnounce(param) {
  const params = {}
  if (param.title) {
    params.title = param.title
  }
  if (param.stats) {
    params.stats = param.stats
  }
  if (param.is_push) {
    params.is_push = param.is_push
  }
  if (param.type) {
    if (param.type === '1') {
      params.mobile = param.member_mobile
    } else {
      params.mobile = 0
    }
    params.type = param.type
  }
  if (param.content) {
    params.content = param.content
  }

  if (params) {
    const paramStr = genParam(params)

    return request({
      url: '/admin/announce/' + param.id + '/edit',
      method: 'put',
      data: paramStr
    })
  }
}

export function deleteAnnounce(param) {
  return request({
    url: '/admin/announce/' + param.id + '/delete',
    method: 'delete'
  })
}
